package com.foamsnet.smstats;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.Phones;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

class SMStats implements Comparable
{
	private int count;
	private int parts;
	private String name;
	
	public SMStats(int a, int b, String c)
	{
		count = a;
		parts = b;
		name = c;
	}
	
	public String getText()
	{
		return name + " - " + count + "/" + parts + "\n";
	}
	
	public int getCount()
	{
		return count;
	}

	@Override
	public int compareTo(Object another) {
		SMStats temp = (SMStats)another;
		if(count > temp.getCount())
			return -1;
		else if(count < temp.getCount())
			return 1;
		else
			return 0;
	}
}

public class ShowStats extends Activity {
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) 
	    {
	    	case R.id.about:
	    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    		builder.setMessage("SMStats!\nBy FoamsNet!\nhttp://code.google.com/p/smstats/\nhttp://foamsnet.com/smstats\n\n:-)");
	    		AlertDialog alert = builder.create();
	    		alert.show();
	    		break;
	    	case R.id.exit:
	    		System.exit(0);
	    		break;
	    }
		return true;
	}
	
	@SuppressWarnings("deprecation")
	private String getContactName(String number)
	{
		Uri uri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(number));
		Context context = getApplicationContext();
		String name = number;
		Cursor cursor = context.getContentResolver().query(uri, 
		                    new String[] { Phones.DISPLAY_NAME }, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
		    name = cursor.getString(cursor.getColumnIndex(Phones.DISPLAY_NAME));
		    cursor.close();
		}
		return name;
	}
	
    @SuppressWarnings("unchecked")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView tv = (TextView) findViewById(R.id.tv);
        String text=new String();
        //read the values
        SharedPreferences settings = getSharedPreferences("SMStats", Context.MODE_PRIVATE);
        text += "Overall received count: " + settings.getInt("sent_count", 0) + "\n";
        text += "Overall received parts: " + settings.getInt("sent_parts_count", 0) + "\n\n";
        text += "Message count by recipient: \n";
        Map<String, Integer> counts = (Map<String, Integer>) settings.getAll();
        Iterator<String> ite = counts.keySet().iterator();
        ArrayList<SMStats> list = new ArrayList<SMStats>();
        while(ite.hasNext())
        {
        	String temp = ite.next();
        	if(temp.equals("sent_count") || temp.equals("sent_parts_count") || temp.charAt(0)=='a')
        		continue;
        	list.add(new SMStats(settings.getInt(temp, 0), settings.getInt("a" + temp, 0), getContactName(temp)));
        }
        Collections.sort(list);
        Iterator<SMStats> site = list.iterator();
        while(site.hasNext())
        	text += site.next().getText();
        tv.setText(text.toCharArray(), 0, text.length());
    }
}